package org.pattern.Interceptor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TargetInvocation {
    private List<Interceptor> interceptorList = new ArrayList<>();
    private Iterator<Interceptor> interceptors;
    private Target target;
    private Request request;

    public Response invoke(){
        if (interceptors.hasNext()){
            Interceptor next = interceptors.next();
            next.intercept(this);
        }
        return target.execute(request);
    }

    public void addInterceptor(Interceptor interceptor){
        interceptorList.add(interceptor);
        interceptors = interceptorList.iterator();
    }

    public Target getTarget() {
        return target;
    }

    public void setTarget(Target target) {
        this.target = target;
    }
}
